package ${cfg.basePackage}.module.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import ${cfg.basePackage}.commons.mybatis.MiPage;
import ${cfg.basePackage}.commons.mybatis.Wrapper;
import ${cfg.basePackage}.commons.utils.R;
import ${cfg.basePackage}.mybatis.gen.dao.${entity}Dao;
import ${cfg.basePackage}.mybatis.gen.entity.${entity};
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * ${table.comment!}服务实现类
 * </p>
 *
 * @author TODO
 * @date ${date}
 */
@Slf4j
@Service
public class ${entity}ServiceImpl implements ${entity}Service {
    @Autowired
    private ${entity}Dao ${table.entityPath}Dao;

    //添加一条数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insert(${entity} param) {
        ${entity} ${table.entityPath} = new ${entity}();

        //参考用法  添加数据时，检查属性重复
        //        ${table.entityPath}.setName(param.getName());
        //        if (${table.entityPath}Dao.isHave(${table.entityPath})) {
        //            R.byError(ResMsg.ERROR_NAME);
        //        }

        //添加数据
        ${table.entityPath}.setId(nextId());
        //${table.entityPath}.setName(param.getName());
        //${table.entityPath}.setLogo(param.getLogo());
        //${table.entityPath}.setSort(param.getSort());
        //${table.entityPath}.setFileIds(param.getFileIds());
        //${table.entityPath}.setTypeIds(param.getTypeIds());
        ${table.entityPath}Dao.save(${table.entityPath});

        return R.bySuccess();
    }

    //删除数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R delete(Long[] ids) {
        ${table.entityPath}Dao.removeByIds(Arrays.asList(ids));
        return R.bySuccess();
    }

    //修改一条数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R update(${entity} param) {

        //参考用法   修改数据时，检查属性重复
        //        Wrapper<${entity}> wrapper = new Wrapper<>();
        //        wrapper.eq(${entity}.NAME, param.getName());
        //        wrapper.ne(${entity}.ID, param.getId());
        //        ${table.entityPath}Dao.isHave(wrapper);
        //        if (${table.entityPath}Dao.isHave(wrapper)) {
        //            R.byError(ResMsg.ERROR_NAME);
        //        }

        //修改 数据
        ${entity} ${table.entityPath} = new ${entity}();
        ${table.entityPath}.setId(param.getId());
        //${table.entityPath}.setName(param.getName());
        //${table.entityPath}.setLogo(param.getLogo());
        //${table.entityPath}.setSort(param.getSort());
        //${table.entityPath}.setFileIds(param.getFileIds());
        //${table.entityPath}.setTypeIds(param.getTypeIds());
        ${table.entityPath}Dao.updateById(${table.entityPath});

        return R.bySuccess();
    }

    //查询数据集合
    @Override
    public R list(${entity} param) {
        Wrapper<${entity}> wrapper = new Wrapper<>();

        //      参考用法  查询前几条数据，指定几条数据
        //      limit 条件
        //      ${table.entityPath}Dao.selectList(wrapper,10);
        //      limit offset  条件
        //      ${table.entityPath}Dao.selectList(wrapper,10,10);

        //wrapper.orderByAsc(${entity}.SORT, ${entity}.CREATE_TIME);
        List<${entity}> ${table.entityPath}s = ${table.entityPath}Dao.list(wrapper);
        return R.bySuccess(${table.entityPath}s);
    }

    //分页查询数据
    @Override
    public R page(R param) {
        //判断不为空时添加参数
        Wrapper<${entity}> wrapper = new Wrapper<>();
        //wrapper.orderByAsc(${entity}.SORT, ${entity}.CREATE_TIME);
        eq(${entity}.ID, param.get("id"), wrapper);
        //like(${entity}.NAME, param.get("name"), wrapper);

        //参考用法  between and
        //        if (ValidateKit.isNotBlank(param.get("time")) && param.getJSONArray("time").size() == 2) {
        //            wrapper.between(${entity}.CREATE_TIME, param.getJSONArray("time").get(0), param.getJSONArray("time").get(1));
        //        }
        IPage<${entity}> ${table.entityPath}IPage = ${table.entityPath}Dao.page(new MiPage<>(param), wrapper);
        return R.bySuccess(${table.entityPath}IPage);
    }

    //查询一条数据
    @Override
    public R one(${entity} param) {
        Wrapper<${entity}> wrapper = new Wrapper<>();
        //参考用法--       参数不能为空
        //        Long id = requireLong(param.get("id"));
        //        Object name = require(param.get("name"));
        //        wrapper.eq(${entity}.ID, id);
        //        wrapper.eq(${entity}.NAME, name);

        //参数可以为空
        //eq(${entity}.ID, param.get("id"), wrapper);
        //eq(${entity}.NAME, param.get("name"), wrapper);

        ${entity} ${table.entityPath} = ${table.entityPath}Dao.getOne(wrapper);

        return R.bySuccess(${table.entityPath});
    }
}